﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Edu;

namespace ZHDJ.Web.Portal.Edu
{
    public partial class PartyEduCoursewareList : System.Web.UI.Page
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }

        private string orderby
        {
            get { return this.Request["orderby"] ?? ""; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            this.hforderby.Value = this.orderby;
            hfTotalCount.Value = PartyEduCourseware.GetPartyEduCoursewareCount().Tables[0].Rows.Count.ToString();
        }

        private void WriteList()
        {
            StringBuilder sbHtml = new StringBuilder();
            DataTable dt = PartyEduCourseware.GetPartyEduCoursewareByOrder(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize), this.orderby).Tables[0];

            if (this.orderby== "ClickCount")
                sbHtml.Append("<li><a>课程名称</a><span>点击数</span></li>");
            else
                sbHtml.Append("<li><a>课程名称</a><span>添加时间</span></li>");


            foreach (DataRow dr in dt.Rows)
            {
                sbHtml.Append("<li>");
                sbHtml.Append("<a target =\"_blank\"  href=\"PartyEduCoursewareDetail.aspx?id=" + dr["ID"] + "\" >" + CutLongTitle(Convert.ToString(dr["Title"]), 52) + "</a>");
                if (this.orderby == "ClickCount")
                    sbHtml.Append("<span>" + Convert.ToString(dr["ClickCount"]) + "</span>");
                else
                    sbHtml.Append("<span>" + Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd") + "</span>");
                sbHtml.Append("</li>");
            }

            Response.Clear();
            Response.Write(sbHtml.ToString());
            Response.End();
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }
    }
}